<?php
class ViewHelper extends YucyAbsHelper
{
	protected $params = array();
	
	protected $value = array();
	
	protected $contentType = '';
	
	protected $charset = '';
	
	protected $content = '';
	
	protected $template = '';
	
	
	public function __construct(){}
	
    
	/**
	 * 
	 * 返回视图对象
	 * @access Public
	 * @param String $engine
	 * @return View
	 */
	public static function factory($engine = '')
	{
		if ($engine == '') return new self();
		
		$class_name = ucfirst(strtolower($engine)) . 'View';
		if (class_exists($class_name, false)) return new $class_name;
		
		require 'View' . DS . $class_name . '.php';
		return new $class_name;
	}
	
	
	public function getContentType()
	{
		if ($this->contentType) return $this->contentType;
		
		return YucyConfig::get('view_content_type');
	}
	
	
	public function setContentType($contentType)
	{
		$this->contentType = (string)$contentType;
		return $this;
	}
	
	
	public function getCharset()
	{
		if ($this->charset) return $this->charset;
		
		return YucyConfig::get('view_charset');
	}
	
	
	public function setCharset($charset)
	{
		$this->charset = (string)$charset;
		return $this;
	}
	
	
	public function setContent($content)
	{
		$this->content = $content;
		return $this;
	}
	
	
	public function getContent()
	{
		return $this->content;
	}
	
	
	/**
	 * 
	 * 设置参数
	 * @access Public
	 * @param String $name
	 * @param Mixed $value
	 */
	public function setParam($name, $value)
	{
		$this->params[$name] = $value;
	}
	
	
	/**
	 * 
	 * 获取参数
	 * @access Public
	 * @param String $name
	 * @return Mixed
	 */
	public function getParam($name)
	{
		return isset($this->params[$name]) ? $this->params[$name] : NULL;
	}
	
	
	/**
	 * 
	 * 设置模版
	 * @access Public
	 * @param String $template
	 */
	public function setTemplate($template)
	{
		$this->template = $template;
	}
	
	
	/**
	 * 
	 * 获得模版
	 * @access Public
	 * @return String
	 */
	public function getTemplate()
	{
		return $this->template;
	}
	
	
	/**
     * 模板变量赋值
     * 
     * @access Public
     * @param Mixed $name
     * @param Mixed $value
     */
    public function assign($name, $value = '')
	{
        if (!is_object($name) && !is_array($name))
		{
			$this->value[$name] = $value;
			return ;
        }
        
		foreach($name as $key =>$val)
		{
			$this->value[$key] = $val;
		}
    }
    
    
    /**
     * 取赋值变量的值
     * @access Public
     * @param Mixed $name
     * @return Mixed
     */
    public function get($name)
    {
    	return isset($this->value[$name]) ? $this->value[$name] : null;
    }
    
    
    public function output()
    {
    	Yucy::getApp()->getResponse()->setContent($this->getContent())->send();
    }
}